//
//  OfferSiteViewController.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 16/1/14.
//  Copyright © 2016年 GripMeMory. All rights reserved.
//

#import "OfferSiteViewController.h"

#import "PopSelectionCell.h"

#import "DottedView.h"

#import "ObjectDetailView.h"

#define kNoticeLabelHeight 60 / 2
#define kOfferViewHeight 215 / 2
#define kOfferViewLeftMargin 35 / 2
#define kOfferViewTopMargin 15 / 2
#define kStartingLabelWidth 240 / 2
#define kStartingLabelHeight 40 / 2
#define kStartingSeparateMargin 10 / 2
#define kStartingLabelRightMargin 34 / 2
#define kPriceLabelHeight 105 / 2
#define kPriorityLabelWidth 150 / 2
#define kMinusLabelLeftMargin 27 / 2
#define kMinusLabelTopMargin 30 / 2
#define kMinusLabelRightMargin 20 / 2
#define kMinusLabelWidth 50 / 2
#define kDottedViewTopMargin 10 / 2
#define kTextFieldLeftMargin 50 / 2

NSString * const SelectionCellName = @"popCellName";

int viewFlag = 1;

@interface OfferSiteViewController () <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, UIScrollViewDelegate>

@end

@implementation OfferSiteViewController

{
    WYPopoverController *_popController;
    UITableViewController *_tbc;
    NSArray *_selectionArr;
    
    UIView *_offerView;
    UILabel *_startingPriceLabel;
    UILabel *_priceLabel;
    UILabel *_bidingLabel;
    UITextField *_bidingTextField;
    
    UIScrollView *_scrollView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customNav];
    [self customMainView];
    [self customOfferView];
    [self customNoticeView];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)customNav{
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(bbiClick:)];
    self.navigationItem.leftBarButtonItem = bbi;
    
    UIBarButtonItem *bbi2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"闹钟1"] style:UIBarButtonItemStylePlain target:self action:@selector(reminderClick:)];
    
    UIBarButtonItem *bbi3 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"星星1"] style:UIBarButtonItemStylePlain target:self action:@selector(starredClick:)];
//    [bbi3 setBackgroundImage:[UIImage imageNamed:@"星星1"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
//    [bbi3 setBackgroundImage:[UIImage imageNamed:@"星星"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    
    UIBarButtonItem *bbi4 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"更多"] style:UIBarButtonItemStylePlain target:self action:@selector(bbiShowList:)];
    self.navigationItem.rightBarButtonItems = @[bbi4, bbi3, bbi2];
}

/**
 *  @author GripMeMory, 16-02-19 17:02:33
 *
 *  主视图，一个scrollview
 */
- (void)customMainView{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, UISCREENWIDTH, self.view.height - kOfferViewHeight - kNoticeLabelHeight)];
    _scrollView.backgroundColor = [UIColor whiteColor];
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(UISCREENWIDTH, (self.view.height - kOfferViewHeight -  kNoticeLabelHeight - kNavigationBarHeight));
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    
    [self changeViewWithTag:viewFlag];
    
    _scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        viewFlag -= 1;
        NSLog(@"%d", viewFlag);
        [self boundTheTag];
        [self changeViewWithTag:viewFlag];
        [_scrollView.mj_header endRefreshing];
    }];
    
    _scrollView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        viewFlag += 1;
        NSLog(@"%d", viewFlag);
        [self boundTheTag];
        [self changeViewWithTag:viewFlag];
        [_scrollView.mj_footer endRefreshing];
    }];
    
    
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -kNavigationBarHeight - kOffsiteViewHeight, UISCREENWIDTH, kOffsiteViewHeight)];
//    view.backgroundColor = [UIColor redColor];
//    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, _scrollView.height, UISCREENWIDTH, kOffsiteViewHeight)];
//    view2.backgroundColor = [UIColor yellowColor];
//    [_scrollView addSubview:view];
//    [_scrollView addSubview:view2];
    
//    UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREENWIDTH, _scrollView.height)];
//    mainView.backgroundColor = [UIColor whiteColor];
//    mainView.tag = 101;
//    [_scrollView addSubview:mainView];
    
    [self.view addSubview:_scrollView];
}

- (void)customOfferView{
    _offerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bottom - kNoticeLabelHeight - kOfferViewHeight, UISCREENWIDTH, kOfferViewHeight)];
    _offerView.backgroundColor = RGB_COLOR(@"#283643");
    [self.view addSubview:_offerView];
    
    _startingPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(kOfferViewLeftMargin, kOfferViewTopMargin, kStartingLabelWidth, kStartingLabelHeight)];
    _startingPriceLabel.backgroundColor = [UIColor clearColor];
    _startingPriceLabel.textColor = RGB_COLOR(@"#c2cace");
    _startingPriceLabel.layer.cornerRadius = 3;
    _startingPriceLabel.layer.borderWidth = 2;
    _startingPriceLabel.layer.borderColor = [RGB_COLOR(@"#c2cace") CGColor];
    _startingPriceLabel.text = @"起始价：500,000.00";
    _startingPriceLabel.textAlignment = NSTextAlignmentCenter;
    _startingPriceLabel.adjustsFontSizeToFitWidth = YES;
    [_offerView addSubview:_startingPriceLabel];
    
    UILabel *priceNoticeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_startingPriceLabel.left, _startingPriceLabel.bottom + kStartingSeparateMargin, kStartingLabelWidth, kStartingLabelHeight)];
    priceNoticeLabel.text = @"(1-500倍报价区间)";
    priceNoticeLabel.backgroundColor = [UIColor clearColor];
    priceNoticeLabel.textColor = RGB_COLOR(@"#646668");
    priceNoticeLabel.textAlignment = NSTextAlignmentCenter;
    priceNoticeLabel.adjustsFontSizeToFitWidth = YES;
    [_offerView addSubview:priceNoticeLabel];
    
    _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(_startingPriceLabel.left, priceNoticeLabel.bottom + kStartingSeparateMargin, kStartingLabelWidth, kPriceLabelHeight)];
    _priceLabel.text = @"出价金额：\n￥888,888.00元";
    _priceLabel.numberOfLines = 2;
    _priceLabel.textAlignment = NSTextAlignmentCenter;
    _priceLabel.textColor = [UIColor whiteColor];
    _priceLabel.backgroundColor = RGB_COLOR(@"#d91f00");
    _priceLabel.adjustsFontSizeToFitWidth = YES;
    [_offerView addSubview:_priceLabel];
    
    UILabel *buyNowPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(UISCREENWIDTH - kOfferViewLeftMargin - kPriorityLabelWidth, kOfferViewTopMargin, kPriorityLabelWidth, kStartingLabelHeight)];
    buyNowPriceLabel.text = @"一口价";
    buyNowPriceLabel.backgroundColor = RGB_COLOR(@"#e8f0f2");
    buyNowPriceLabel.layer.cornerRadius = 3;
    buyNowPriceLabel.textAlignment = NSTextAlignmentCenter;
    buyNowPriceLabel.clipsToBounds = YES;
    UITapGestureRecognizer *buyNowPriceTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buyNowPrice)];
    [buyNowPriceLabel addGestureRecognizer:buyNowPriceTap];
    [_offerView addSubview:buyNowPriceLabel];
    
    UILabel *priorityLabel = [[UILabel alloc] initWithFrame:CGRectMake(_startingPriceLabel.right + kStartingLabelRightMargin, kOfferViewTopMargin, UISCREENWIDTH - 2 * kOfferViewLeftMargin - 2 * kStartingLabelRightMargin - kPriorityLabelWidth - _startingPriceLabel.width, kStartingLabelHeight)];
    priorityLabel.text = @"优先使用权";
    priorityLabel.textColor = RGB_COLOR(@"#c2cace");
    priorityLabel.layer.cornerRadius = 3;
    priorityLabel.layer.borderWidth = 2;
    priorityLabel.layer.borderColor = [RGB_COLOR(@"#c2cace") CGColor];
    priorityLabel.textAlignment = NSTextAlignmentCenter;
    priorityLabel.adjustsFontSizeToFitWidth = YES;
    [_offerView addSubview:priorityLabel];
    
    UILabel *minusLabel = [[UILabel alloc] initWithFrame:CGRectMake(_startingPriceLabel.right + kMinusLabelLeftMargin, _startingPriceLabel.bottom + kMinusLabelTopMargin, kMinusLabelWidth, kMinusLabelWidth)];
    minusLabel.text = @"-";
    minusLabel.textAlignment = NSTextAlignmentCenter;
    minusLabel.backgroundColor = [UIColor whiteColor];
    UITapGestureRecognizer *minusTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(minusPrice)];
    [minusLabel addGestureRecognizer:minusTap];
    minusLabel.userInteractionEnabled = YES;
    [_offerView addSubview:minusLabel];
    
    UILabel *addLabel = [[UILabel alloc] initWithFrame:CGRectMake(buyNowPriceLabel.right - kMinusLabelWidth, minusLabel.top, kMinusLabelWidth, kMinusLabelWidth)];
    addLabel.text = @"+";
    addLabel.textAlignment = NSTextAlignmentCenter;
    addLabel.backgroundColor = [UIColor whiteColor];
    UITapGestureRecognizer *addTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addPrice)];
    [addLabel addGestureRecognizer:addTap];
    addLabel.userInteractionEnabled = YES;
    [_offerView addSubview:addLabel];
    
    _bidingLabel = [[UILabel alloc] initWithFrame:CGRectMake(minusLabel.right + kMinusLabelRightMargin, minusLabel.top, UISCREENWIDTH - 2 * kOfferViewLeftMargin - kStartingLabelWidth - 2 * kMinusLabelRightMargin - kMinusLabelLeftMargin - 2 * kMinusLabelWidth, kMinusLabelWidth)];
    _bidingLabel.text = @"0.00";
    _bidingLabel.textAlignment = NSTextAlignmentCenter;
    _bidingLabel.backgroundColor = [UIColor whiteColor];
    [_offerView addSubview:_bidingLabel];
    
    DottedView *dottedView = [[DottedView alloc] initWithFrame:CGRectMake(minusLabel.left, _bidingLabel.bottom + kDottedViewTopMargin, UISCREENWIDTH - 2 * kOfferViewLeftMargin - kStartingLabelRightMargin - kStartingLabelWidth, 1)];
    [dottedView layoutIfNeeded];
    [_offerView addSubview:dottedView];
    
    _bidingTextField = [[UITextField alloc] initWithFrame:CGRectMake(_startingPriceLabel.right + kTextFieldLeftMargin, dottedView.bottom + kDottedViewTopMargin, (UISCREENWIDTH - 2 * kOfferViewLeftMargin - kStartingLabelWidth - kTextFieldLeftMargin - kMinusLabelRightMargin) / 2, kStartingLabelHeight)];
    _bidingTextField.delegate = self;
    _bidingTextField.backgroundColor = [UIColor whiteColor];
    _bidingTextField.textAlignment = NSTextAlignmentCenter;
    [_bidingTextField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"1~500" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}]];
    [_offerView addSubview:_bidingTextField];
    
    UILabel *confirmPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(_bidingTextField.right + kMinusLabelRightMargin, _bidingTextField.top, (UISCREENWIDTH - 2 * kOfferViewLeftMargin - kStartingLabelWidth - kTextFieldLeftMargin - kMinusLabelRightMargin) / 2, kStartingLabelHeight)];
    confirmPriceLabel.text = @"确认出价";
    confirmPriceLabel.layer.cornerRadius = 3;
    confirmPriceLabel.textAlignment = NSTextAlignmentCenter;
    confirmPriceLabel.backgroundColor = RGB_COLOR(@"#d91f00");
    confirmPriceLabel.textColor = [UIColor whiteColor];
    [_offerView addSubview:confirmPriceLabel];
}

- (void)customNoticeView{
    UILabel *noticeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.bottom - kNoticeLabelHeight, UISCREENWIDTH, kNoticeLabelHeight)];
    noticeLabel.backgroundColor = RGB_COLOR(@"#d5a540");
    noticeLabel.text = @"竞价即将结束, 请尽快出价";
    noticeLabel.textColor = [UIColor whiteColor];
    noticeLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:noticeLabel];
}

#pragma mark - UITableView DataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PopSelectionCell *cell = [tableView dequeueReusableCellWithIdentifier:SelectionCellName forIndexPath:indexPath];
    if (!cell) {
        cell = [[PopSelectionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SelectionCellName];
    }
    [cell configureCellWithModel:_selectionArr[indexPath.row]];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _selectionArr.count;
}

#pragma mark - UITableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

#pragma mark - Action

- (void)bbiClick:(UIBarButtonItem *)bbi{
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)reminderClick:(UIBarButtonItem *)bbi{
    if ([bbi.image isEqual:[UIImage imageNamed:@"闹钟1"]]) {
        bbi.image = [UIImage imageNamed:@"闹钟"];
    }else{
        bbi.image = [UIImage imageNamed:@"闹钟1"];
    }
}

- (void)starredClick:(UIBarButtonItem *)bbi{
    if ([bbi.image isEqual:[UIImage imageNamed:@"星星1"]]) {
        bbi.image = [UIImage imageNamed:@"星星"];
    }else{
        bbi.image = [UIImage imageNamed:@"星星1"];
    }
}

- (void)buyNowPrice{
    NSLog(@"buyNow");
}

- (void)minusPrice{
    CGFloat price = [_bidingLabel.text floatValue];
    if (price - 100 >= 0) {
        price -= 100;
    }
    _bidingLabel.text = [NSString stringWithFormat:@"%.2f", price];
}

- (void)addPrice{
    CGFloat price = [_bidingLabel.text floatValue];
    price += 100;
    _bidingLabel.text = [NSString stringWithFormat:@"%.2f", price];
}

- (void)bbiShowList:(UIBarButtonItem *)bbi{
    _tbc = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    _tbc.tableView.tag = 1;
    
    _selectionArr = @[@"首页", @"消息", @"分享"];
    
    _tbc.tableView.dataSource = self;
    _tbc.tableView.delegate = self;
    
    _tbc.tableView.backgroundColor = [UIColor darkGrayColor];
    _tbc.tableView.scrollEnabled = NO;
    _tbc.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tbc.preferredContentSize = CGSizeMake(100, 150);
    [_tbc.tableView registerClass:[PopSelectionCell class] forCellReuseIdentifier:SelectionCellName];
    
    
    
    //Popover appearance
    WYPopoverBackgroundView *appearance = [WYPopoverBackgroundView appearance];
    [appearance setInnerCornerRadius:15];
    [appearance setOuterCornerRadius:15];
    [appearance setOuterStrokeColor:[UIColor clearColor]];
    [appearance setOuterShadowOffset:CGSizeZero];
    [appearance setFillTopColor:[UIColor clearColor]];
    [appearance setTintColor:[UIColor clearColor]];
    [appearance setViewContentInsets:UIEdgeInsetsZero];
    [appearance setBorderWidth:0];
    
    _popController = [[WYPopoverController alloc] initWithContentViewController:_tbc];
    [_popController presentPopoverFromBarButtonItem:bbi permittedArrowDirections:WYPopoverArrowDirectionNone animated:YES];
}

- (void)boundTheTag{
    if (viewFlag == 0) {
        viewFlag = 1;
    }else if(viewFlag == 4){
        viewFlag = 3;
    }else{
        return;
    }
}

- (void)changeViewWithTag:(int)viewTag{
    
    for (UIView *view in _scrollView.subviews) {
        if (view.tag == 101) {
            [view removeFromSuperview];
        }
    }
    
    if (viewFlag == 1) {
        ObjectDetailView *view = [[ObjectDetailView alloc] initWithFrame:CGRectMake(0, 0, _scrollView.width, _scrollView.height)];
        view.tag = 101;
        
        ObjectDetailModel *model = [[ObjectDetailModel alloc] init];
        model.objectImage = [UIImage imageNamed:@"bg.jpg"];
        model.objectTitle = @"内蒙古自治区基本建设咨询有限公司蒙A50968(奥迪A6L)转让";
        model.starringCount = @"3653";
        model.currentPrice = @"855,000.00";
        NSDate *date = [NSDate dateWithTimeIntervalSinceNow:173];
        NSDateFormatter *fomatter = [[NSDateFormatter alloc] init];
        fomatter.dateFormat = timeFormatter;
        model.boundTime = [fomatter stringFromDate:date];
        
        [view configureViewWithModel:model];
        [_scrollView addSubview:view];
    }else if(viewFlag == 2){
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _scrollView.width, _scrollView.height)];
        view.tag = 101;
        view.backgroundColor = [UIColor cyanColor];
        [_scrollView addSubview:view];
    }else if(viewFlag == 3){
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _scrollView.width, _scrollView.height)];
        view.tag = 101;
        view.backgroundColor = [UIColor grayColor];
        [_scrollView addSubview:view];
    }
}

@end
